package controller.provider;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @program: SMBMS-two
 * @description:
 * @author: Mr.peng
 * @create: 2020-05-25 10:10
 **/
@Controller
public class Providers {
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    ProviderService providerService = context.getBean("provider", ProviderService.class);
    String name;
//     @RequestMapping("/jsp/provider.do")
//    public String doPost(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        String method = request.getParameter("method");
//         System.out.println(">>>>>>>"+method);
//        if(method != null && method.equals("query")){
//            this.query(request,response);
//        }else if(method != null && method.equals("add")){
//            this.add(request,response);
//        }else if(method != null && method.equals("view")){
//            this.getProviderById(request,response,"jsp/providerview");
//        }else if(method != null && method.equals("modify")){
//            this.getProviderById(request,response,"jsp/providermodify");
//        }else if(method != null && method.equals("modifysave")){
//            this.modify(request,response);
//        }else if(method != null && method.equals("delprovider")){
//            this.delProvider(request,response);
//        }
//        return name;
//    }
    @RequestMapping("/jsp/providerdel.do/{id}")
    private void delProvider(@PathVariable String id, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //String id = request.getParameter("proid");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(!StringUtils.isNullOrEmpty(id)){
            int flag = providerService.deleteProviderById(id);
            if(flag == 0){//删除成功
                resultMap.put("delResult", "true");
            }else if(flag == -1){//删除失败
                resultMap.put("delResult", "false");
            }else if(flag > 0){//该供应商下有订单，不能删除，返回订单数
                resultMap.put("delResult", String.valueOf(flag));
            }
        }else{
            resultMap.put("delResult", "notexit");
        }
        //把resultMap转换成json对象输出
        response.setContentType("application/json");
        PrintWriter outPrintWriter = response.getWriter();
        outPrintWriter.write(JSONArray.toJSONString(resultMap));
        outPrintWriter.flush();
        outPrintWriter.close();
    }

    @RequestMapping("/jsp/providersho.do")
    private String modify(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("||||||||||||||||||||||||");
        String proName = request.getParameter("proName");
        String proCode = request.getParameter("proCode");
        String proContact = request.getParameter("proContact");
        String proPhone = request.getParameter("proPhone");
        String proAddress = request.getParameter("proAddress");
        String proFax = request.getParameter("proFax");
        String proDesc = request.getParameter("proDesc");
        String id = request.getParameter("id");
        Provider provider = new Provider();
        provider.setId(Integer.valueOf(id));
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProFax(proFax);
        provider.setProAddress(proAddress);
        provider.setProDesc(proDesc);
        provider.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setModifyDate(new Date());
        provider.setProName(proName);
        provider.setProCode(proCode);
        boolean flag = false;
        flag = providerService.modify(provider);
        System.out.println("............."+flag);
        if(flag){
            //response.sendRedirect(request.getContextPath()+"/jsp/provider.do?method=query");
             return "redirect:/jsp/provider.do";
        }else{
            //request.getRequestDispatcher("providermodify.jsp").forward(request, response);
            return "jsp/providermodify";
        }
    }


    @RequestMapping("/jsp/providerview.do")
    private String getProviderById(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("proid");
        String method=request.getParameter("method");
        String url=null;
        if(method.equals("view")){
            url="jsp/providerview";
        }else if(method.equals("modify")){
            url="jsp/providermodify";
        }
        if(!StringUtils.isNullOrEmpty(id)){
            Provider provider = null;
            provider = providerService.getProviderById(id);
            request.setAttribute("provider", provider);
           // request.getRequestDispatcher(url).forward(request, response);
            return url;
        }
        return null;
    }


    @RequestMapping("/jsp/provideradds.do")
    private String add(HttpServletRequest request)
            throws ServletException, IOException {
        System.out.println("||||||||||||||||||||||||||s");
        String proCode = request.getParameter("proCode");
        String proName = request.getParameter("proName");
        String proContact = request.getParameter("proContact");
        String proPhone = request.getParameter("proPhone");
        String proAddress = request.getParameter("proAddress");
        String proFax = request.getParameter("proFax");
        String proDesc = request.getParameter("proDesc");
        Provider provider = new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProFax(proFax);
        provider.setProAddress(proAddress);
        provider.setProDesc(proDesc);
       // BeanUtils.copyProperties(model,provider);
        provider.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setCreationDate(new Date());
        boolean flag = false;
        flag = providerService.add(provider);
        if(flag){
            //response.sendRedirect(request.getContextPath()+"/jsp/provider.do?method=query");
           return "redirect:/jsp/provider.do";
        }else{
            //request.getRequestDispatcher("provideradd.jsp").forward(request, response);
           return "jsp/provideradd";
        }
    }

    @RequestMapping("/jsp/provider.do")
    private String query(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String queryProName = request.getParameter("queryProName");
        String queryProCode = request.getParameter("queryProCode");
        if(StringUtils.isNullOrEmpty(queryProName)){
            queryProName = "";
        }
        if(StringUtils.isNullOrEmpty(queryProCode)){
            queryProCode = "";
        }
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList(queryProName,queryProCode);
        request.setAttribute("providerList", providerList);
        request.setAttribute("queryProName", queryProName);
        request.setAttribute("queryProCode", queryProCode);
       // request.getRequestDispatcher("providerlist.jsp").forward(request, response);
       return  "jsp/providerlist";
    }
}
